package com.clas.liu.myapplication.presenter.impl;

import android.app.usage.UsageEvents;
import android.os.Handler;
import android.os.Message;

import com.clas.liu.myapplication.model.entity.user.UserData;
import com.clas.liu.myapplication.model.entity.user.UserRoot;
import com.clas.liu.myapplication.model.event.LoginEvent;
import com.clas.liu.myapplication.model.impl.BaseModel;
import com.clas.liu.myapplication.model.impl.LoginModel;
import com.clas.liu.myapplication.presenter.i.ILoginPresenter;
import com.clas.liu.myapplication.view.activity.impl.BaseActivity;
import com.clas.liu.myapplication.view.activity.impl.LoginActivity;

/**
 * Created by liu on 2019/3/5.
 */

public class LoginPresenter extends BasePresenter<LoginActivity, LoginModel> implements ILoginPresenter {

    public LoginPresenter(LoginActivity view) {
        super(view);
    }

    @Override
    protected LoginModel getModel(Handler handler) {
        return new LoginModel(handler);
    }

    @Override
    protected void eventReceive(Message msg) {
        switch (msg.what) {
            case LoginEvent.LOGIN_OK:
                saveUserInfoToDB(((LoginEvent)msg.obj).getUserRoot());
                break;
            case LoginEvent.LOGIN_FAIL:
                mView.failBecauseNetReturn(msg.what);
                break;
            case LoginEvent.EXITED_OK:
                UserRoot root = ((LoginEvent)msg.obj).getUserRoot();
                mView.loginReturn(root.getCode(),root);
                break;
            case LoginEvent.EXITED_FAIL:
                mView.failBecauseNetReturn(msg.what);
                break;
        }
    }

    private void saveUserInfoToDB(UserRoot root) {
        mView.loginReturn(root.getCode(),root);
    }

    @Override
    public void login(String username, String password) {
        mModel.login(username, password);
    }

    @Override
    public void loginReturn(int id) {

    }

    @Override
    public void phoneIsExited(String Phone) {
        mModel.phoneIsExited(Phone);
    }
}
